from flask import Flask
from config import BaseConfig
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from authlib.integrations.flask_client import OAuth
from database import db
from flask_migrate import Migrate

from apps.user import bp as user_bp
from apps.form import bp as form_bp
from middleware import check_token_expiry

app = Flask(__name__, static_folder='files', static_url_path='/files')
app.config.from_object(BaseConfig)

CORS(app)

oauth = OAuth(app)

google = oauth.register(
    name='google',
    client_id=BaseConfig.GOOGLE_CLIENT_ID,
    client_secret=BaseConfig.GOOGLE_CLIENT_SECRET,
    access_token_url='https://accounts.google.com/o/oauth2/token',
    access_token_params=None,
    authorize_url='https://accounts.google.com/o/oauth2/auth',
    authorize_params=None,
    api_base_url='https://www.googleapis.com/oauth2/v1/',
    client_kwargs={'scope': 'email profile'},
)

jwt = JWTManager(app)


@app.before_request
def before_request():
    result = check_token_expiry()
    if result is not None:
        return result


db.init_app(app)
migrate = Migrate(app, db)

app.register_blueprint(user_bp)
app.register_blueprint(form_bp)

if __name__ == '__main__':
    app.run()
